package org.hyl.javabase.tree;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Tree {
	User 顶层;
	static final int 左区 = 1;
	static final int 右区 = 2;
	static final BigDecimal bd0 = new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd04 = new BigDecimal(0.04).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd02 = new BigDecimal(0.02).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd01 = new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd05 = new BigDecimal(0.05).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd03 = new BigDecimal(0.03).setScale(2, BigDecimal.ROUND_HALF_UP);
	static final BigDecimal bd1 = new BigDecimal(0.1).setScale(2, BigDecimal.ROUND_HALF_UP);
	List<String> 交易记录 = new ArrayList<>();
	List<User> 用户列表 = new ArrayList<User>();

	public Tree() {
		顶层 = new User("root");
		用户列表.add(顶层);
	}

	/**
	 * 
	 * <p>Title: addUser</p>
	 * <p>Description:  添加新用户 挂出关系树</p>
	 * @param 区
	 * @param 挂载
	 * @param 新用户
	 * @return
	 * @return :boolean
	 * @throws
	 */
	public boolean addUser(int 区, User father, User 新用户) {
		if (区 == 左区) {
			if (father.左 == null)
				father.左 = 新用户;
			else
				return false;

		} else {
			if (father.右 == null)
				father.右 = 新用户;
			else
				return false;

		}

		新用户.父节点 = father;
		用户列表.add(新用户);
		return true;
	}

	//1-2 级 拿4%  3-4级 拿 2%  5-10级 拿1%

	public void consume2(User a, BigDecimal 消费, int i) {
		if (a.父节点 == null||i>10) {
			return;
		}
		BigDecimal rate = bd0;
		if ( i < 2) {
			rate = bd04;
		} else if (i > 1 && i < 4) {
			rate = bd02;
		} else if (i > 3 && i < 10) {
			rate = bd01;
		}
		i++;
		BigDecimal dd = 消费.multiply(rate).setScale(4, BigDecimal.ROUND_HALF_UP);
		a.父节点.奖金 = a.父节点.奖金.add(dd);
		System.out.println("谁:" + a.父节点.name + "rate:" + rate + ",推荐奖:" + dd);
		consume2(a.父节点, 消费, i);
	}
	//1  , 3  , 5 隔代拿 分别 拿 10%  5%  2% 

	public void consume3(User a, BigDecimal 消费, int i) {
		if (a.父节点 == null||i>4)
			return;
		BigDecimal rate = bd0;
		switch (i) {
		case 0:
			rate = bd1;
			break;
		case 2:
			rate = bd05;
			break;
		case 4:
			rate = bd02;
			break;
		}
		i++;
		if (rate!=bd0) {
			BigDecimal dd = 消费.multiply(rate).setScale(4, BigDecimal.ROUND_HALF_UP);
			a.父节点.奖金 = a.父节点.奖金.add(dd);
			System.out.println("谁:" + a.父节点.name + "rate:" + rate + ",推荐奖:" + dd);
		}
		consume3(a.父节点, 消费, i);
	}

	/**
	 * 
	 * <p>Title: 消费</p>
	 * <p>Description:消费引发算法 :
	 * 第一代 拿 10%
	 * 第二代 拿 5%
	 * 第3~n代 拿 3%
	 * 除开根节点
	 * </p>
	 * @param a
	 * @param 消费
	 * @return :void
	 * @throws
	 */
	public void consume(User a, BigDecimal 消费, BigDecimal 奖金比) {
		BigDecimal rate = 奖金比;
		if (rate.equals(bd0))
			rate = bd1;
		else if (rate.equals(bd1))
			rate = bd05;
		else if (rate.equals(bd05))
			rate = bd03;
		if (a.父节点 == null)
			return;
		BigDecimal dd = 消费.multiply(rate).setScale(4, BigDecimal.ROUND_HALF_UP);
		a.父节点.奖金 = a.父节点.奖金.add(dd);
		System.out.println("谁:" + a.父节点.name + "rate:" + rate + ",推荐奖:" + dd);
		consume(a.父节点, 消费, rate);

	}

	public void consume1(User a, BigDecimal 消费) {
		BigDecimal dd = 消费.multiply(bd1).setScale(4, BigDecimal.ROUND_HALF_UP);
		a.父节点.奖金 = a.父节点.奖金.add(dd);
		a = a.父节点;
		dd = 消费.multiply(bd05).setScale(4, BigDecimal.ROUND_HALF_UP);
		a.父节点.奖金 = a.父节点.奖金.add(dd);
		while (a.父节点 != null) {
			a = a.父节点;
			dd = 消费.multiply(bd03).setScale(4, BigDecimal.ROUND_HALF_UP);
			a.父节点.奖金 = a.父节点.奖金.add(dd);
		}
	}

	public static void main(String[] arg) {
		Tree t = new Tree();

		User a01 = new User("a01");
		t.addUser(左区, t.顶层, a01);
		User a02 = new User("a02");
		t.addUser(右区, t.顶层, a02);
		User a03 = new User("a03");
		t.addUser(左区, a01, a03);
		User a04 = new User("a04");
		t.addUser(右区, a02, a04);
		User a05 = new User("a05");
		t.addUser(左区, a04, a05);
		User a06 = new User("a06");
		t.addUser(左区, a05, a06);

		t.consume(a05, BigDecimal.valueOf(1000), bd0);
		t.consume(a06, BigDecimal.valueOf(1000), bd0);

		//t.hc算法();
		t.马哥算法();

	}

	public void 马哥算法() {
		BigDecimal sum = bd0;
		for (User u : 用户列表) {
			sum = sum.add(u.奖金);
			System.out.println(u.name + ":" + u.奖金);
		}
		System.out.println("所有人的奖金:" + sum);
	}

	public void hc算法() {
		BigDecimal sum = dg求和(顶层);
		System.out.println("所有人的奖金:" + sum);
	}

	public BigDecimal dg求和(User u) {
		System.out.println(u.name + ":" + u.奖金);
		BigDecimal l = bd0;
		BigDecimal r = bd0;
		if (u.左 != null)
			l = dg求和(u.左);
		if (u.右 != null)
			r = dg求和(u.右);
		return u.奖金.add(l).add(r);

	}
}
